/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.PriceListMarketingDAOInterface;
import com.wii.elpro.dao.hb.PriceListMarketingDAOImpl;
import com.wii.elpro.entity.PriceListMarketing;
import com.wii.elpro.entity.User;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author hp02
 */
public class PriceListMarketingGetPLsByCabangAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User users = (User)sess.getAttribute("userSession");       
        if(users==null){
            return mapping.findForward("gagalLogin");            
        }  
        String forward;       
        PriceListMarketingDAOInterface dao = new PriceListMarketingDAOImpl();
        ActionMessages messages = null;
        User user = new User();
        if(sess.getAttribute("userSession")!=null) {
            user = (User) sess.getAttribute("userSession"); 
            try {
                List<PriceListMarketing> pls = dao.getPriceListsByCabang(user.getCabang());
                request.setAttribute("pls", pls);
                if(pls.get(0).getCabang()!=null) {
                    messages = new ActionMessages();
                    messages.add("customMessage", new ActionMessage("customMessage", pls.get(0).getCabang().getKota()+"'s price list"));
                    saveMessages(request, messages);
                }
                forward = "success";
            } catch (Exception e) {
                e.printStackTrace();
                forward = "fail";
            }
        } else {
            messages = new ActionMessages();
            messages.add("customMessage", new ActionMessage("customMessage", "You are not authenticated user."));
            saveMessages(request, messages);
            forward = "invalidUser";
        }
        return mapping.findForward(forward);
    }
}
